package com.example.shopproduct.service.impl;

import com.example.shopcommon.dao.entity.Product;
import com.example.shopproduct.dao.ProductDao;
import com.example.shopproduct.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductDao productDao;

    public Product findByPId(Long id) {
        return productDao.findById(id).get();
    }

    public void deduct(Long pid, int num) {
        Product product = productDao.findById(pid).get();
        if (product.getStock() < num) {
            throw new RuntimeException("库存不足");
        }
        product.setStock(product.getStock() - num);
        productDao.save(product);
    }

}
